<?php
session_start();
$db = new mysqli("127.0.0.1", "root", "", "yeoldeguilde");

$con = mysql_connect("127.0.0.1","root","");
	if (!$con) {
		error_log("Could not connect: " . mysql_error());
	}
mysql_select_db("yeoldeguilde", $con);
$query = 'SELECT lastUpdated FROM information';
$result = mysql_query($query,  $con);			
$row = mysql_fetch_array($result);
$lastUpdated = $row['lastUpdated'];
$readableUpdateTxt = date(" F j, Y @ g:i a ", strtotime($lastUpdated));
//echo $lastUpdated;

?>
<!DOCTYPE html> 
<html>
<head>
	<base target="_blank"/>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1"> 
	<title>Ye Olde Guilde Bank</title> 
	<link rel="stylesheet" href="style.css" />
	<script type="text/javascript" src="http://static.wowhead.com/widgets/power.js"></script>
	<script src="./js/jquery-1.7.1.min.js"></script>
	<script src="./js/jquery.tablesorter.min.js"></script>	
	<script src="./js/jquery.tablesorter.widgets.js"></script>
	
	<script src="./js/backend.js"></script>
</head> 
<body> 

<div id="page">
<?php
outputHeader();
?>

<div id="main-content">

<div class="infoText" id="lastUpdatedTxt">
<?php
echo "Latest update:".$readableUpdateTxt." [GMT]";
?>
</div>
</br>
<div class="infoText" id="proTipTxt">
Protip: Click column headers and/or type a filter to order items! =D
</div>
</br>


<?php
if (!isset($_GET['mail'])) {
	outputItemTable();
} else {
	outputMailTable();
}

//outputHeader(); <a target="_self" href="#" onclick="clickTest()">Test</a>
?>
</div>
<a id="floatingLink" href="#" target="_self" title="Back to Top"><img style="border: none;" src="./images/top.gif"/>Back to Top</a>
</div>
</body>
</html>

<?php


function outputHeader() {
?>
<div id="header">
</div>
<?php
}

function outputItemTable() {
	global $db, $lastUpdated;
	// Item list
	$columns = array("Banker", "Count", "Name", "Quality");
	//print_r($info);
	$query = "SELECT id, banker, itemName, itemID, itemCount, color, icon FROM bankitems WHERE lastUpdated='".$lastUpdated."' ORDER BY banker DESC"; // LIMIT 0, 10
	//echo "<br>".$query."<br>";
	$result = $db->query($query, MYSQLI_STORE_RESULT);

	// start table
	?>
	<table class="tablesorter" id="myTable">
	<thead>
	<tr>
		<th>Banker</th>
		<th>Name</th>		
		<th class="filter-false">Count</th>
		<th style="width: 1%;" class="filter-false">Quality</th>
	</tr>
	</thead>
	<?php
		//foreach ($columns as $column) {
		//	$o .= '<th>'.$column.'</th>';
		//}
	//start body of table
	$o = '<tbody>';
	$js = '<script>';
	
	$itemInfo = array();
	while(list($id, $banker, $itemName, $itemID, $itemCount, $color, $icon) = $result->fetch_row()) {
		switch ($color) {
		case "1eff00":
			$quality = "Uncommon";
			break;
		case "ffffff":
			$quality = "Common";
			break;
		case "0070dd":
			$quality = "Rare";
			break;
		default:
			$quality = "It'sAMystery";
		}
		$o .= '<tr id="'.$id.'"><td>'.$banker.'</td><td><a href="http://old.wowhead.com/item='.$itemID.'"><font class="itemName" color=#'.$color.'>'.$itemName.'</a></font>'.	
		'</td><td class="itemCount" id="'.$id.'-itemCount-'.$itemCount.'">'.$itemCount.'</td><td><font color=#'.$color.'>'.$quality.'</font></td></tr>';
		
		$query = "SELECT * FROM item_template WHERE entry ='".$itemID."'"; 
		$itemResult = $db->query($query, MYSQLI_STORE_RESULT);
		$itemRow = $itemResult->fetch_row();
		array_push($itemInfo, $itemRow);
	}

	$o .= '</tbody></table>';
	$js .= 'var itemInfo = jQuery.parseJSON(\''.json_encode($itemInfo).'\');';
	$js .= '</script>';
	echo $o;
	echo $js;
	//echo "<pre>";
	 
	//echo "</pre>";
}

function outputMailTable() {
	global $db, $lastUpdated;
	// Item list
	$columns = array("Banker", "Count", "Name", "Quality");
	//print_r($info);
	$query = "SELECT id, target, sender, item, date, lastUpdated FROM mail ORDER BY date DESC"; // LIMIT 0, 10
	//echo "<br>".$query."<br>";
	$result = $db->query($query, MYSQLI_STORE_RESULT);

	// start table
	?>
	<table class="tablesorter" id="myTable">
	<thead>
	<tr>
		<th>Target</th>
		<th>Sender</th>		
		<th>Item</th>
		<th class="filter-false">Date</th>
	</tr>
	</thead>
	<?php
		//foreach ($columns as $column) {
		//	$o .= '<th>'.$column.'</th>';
		//}
	//start body of table
	$o = '<tbody>';

	while(list($id, $target, $sender, $item, $date) = $result->fetch_row()) {
		
		$o .= '<tr id="'.$id.'"><td>'.$target.'</td><td>'.$sender.'</td><td>'.$item.'</td><td>'.$date.'</td></tr>';

	}

	$o .= '</tbody></table>';

	echo $o;
}